﻿using System.Diagnostics;

namespace BordEdgeFindTest
{
    internal class Program
    {
        private static void CreateDirectoryIfNotExists(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        private static List<string> GetFileNames(string fileDir)
        {
            List<string> fileNames = new List<string>();
            foreach (string file in Directory.GetFiles(fileDir, "*.bmp"))
            {
                fileNames.Add(Path.GetFileName(file));
            }
            return fileNames;
        }
        static void Main(string[] args)
        {
            string filePath = @"C:\Fioman\Raw\FindBoardEdge";
            string keepDir, fileDir;
            if (File.Exists(filePath))
            {
                fileDir = Path.GetDirectoryName(filePath)!;
            }
            else if (!Directory.Exists(filePath))
            {
                Console.WriteLine($"{filePath}目录不存在,请重新填写目录");
                return;
            }
            else
            {
                fileDir = filePath;
            }
            keepDir = filePath;

            // 创建保存结果的目录
            string keepOkPath = Path.Combine(keepDir, "Ok");
            string keepOkResPath = Path.Combine(keepDir, "OkRes");
            string keepFailPath = Path.Combine(keepDir, "fail");

            CreateDirectoryIfNotExists(keepOkPath);
            CreateDirectoryIfNotExists(keepOkResPath);
            CreateDirectoryIfNotExists(keepFailPath);

            List<string> fileNames = GetFileNames(fileDir);

            int indexStart = 1;
            string filePathReal;
            foreach (var fileName in fileNames)
            {
                Console.WriteLine($"识别第 {indexStart++} 张图,图像名称: {fileName}, 识别开始 ");
                Stopwatch sw = Stopwatch.StartNew();
                filePathReal = Path.Combine(keepDir, fileName);
                Mat imageSrc = Cv2.ImRead(filePathReal, ImreadModes.Grayscale);
                OperateResult<(Point[], Mat)> result = ImageProcess.FindBoardEdge(imageSrc);
                Point[] pointRes = new Point[2];
                Mat imageRes = new Mat();
                (pointRes, imageRes) = result.Data;
                if (result.State)
                {
                    sw.Stop();
                    Console.WriteLine($"找板边成功,算法耗时:{sw.ElapsedMilliseconds / 1000.0:F3} 秒");
                    Cv2.ImWrite(Path.Combine(keepFailPath, fileName), imageSrc);
                    File.Delete(filePathReal);
                }
                else
                {
                    sw.Stop();
                    Console.WriteLine($"找板边失败,失败原因: {result.Message},算法耗时:{sw.Elapsed.Seconds / 1000.0:F3} 秒");
                    Cv2.ImWrite(Path.Combine(keepOkPath, fileName), imageSrc);
                    Cv2.ImWrite(Path.Combine(keepOkResPath, fileName), imageRes);
                    File.Delete(filePathReal);
                }
            }

        }


    }
}